﻿<?php

	class Controller_Users extends Controller_Base
	{
		function index()
		{
			//$this->assignData('zxc','HELLO'); // добавление данных в .tpl
			//$this->assignData('genres', WebApp::app()->db->select(array('*'),array('genres')));// работа с БД
			$db = WebApp::app()->db;
			$users = $db->select(array('*'), array('users', 'maskassignment'), 'users.id = maskassignment.user_id');
			$this->assignData('users', $users);
		}
		function view()
		{
			$db = WebApp::app()->db;
			$user = $db->select(array('*'),array('users'),'id = '.$this->args[0]);
			$user = $user[0];
			$roles = $db->select(array('maskassignment.itemname'), array('maskassignment'), 'maskassignment.user_id = '.$user->id);
			$this->assignData('user', $user);
			$this->assignData('roles', $roles);
			$this->assignData('path', WebApp::app()->getBaseUrl());
		}
		
		function editRole()
		{
			$db = WebApp::app()->db;
			
			if(isset($_POST['ajax']))
			{
				switch($_POST['type'])
				{
					case "add":{
						if($_POST['roleName'] == "null")
						{
							echo true;
							exit;
						}
							
						$db->insert("maskassignment", array(
																"itemname" => $_POST['roleName'],
																"user_id" => $this->args[0]
															));

					}break;
					case "del":{
						if($_POST['roleName'] == "null")
						{
							echo true;
							exit;	
						}
						
						$db->delete("maskassignment", "itemname = '" . $_POST['roleName'] . "' and user_id = " . $this->args[0]);
						
						
					}break;
				}
				
				echo true;
				exit;
			}
			
			$user = $db->select(array('*'),array('users'),'id = '.$this->args[0]);
			$user = $user[0];
			$user_roles = $db->select(array('maskassignment.itemname'), array('maskassignment'), 'maskassignment.user_id = '.$user->id);
			
			$rolesAll = $db->select(array('name'), array('maskitem'), "maskitem.type = 2 and maskitem.name <> 'Guest'");
			
			$roleNew = array();
			foreach($rolesAll as $roleAll)
			{
				$flag = false;
				foreach($user_roles as $user_role)
				{
					if($user_role->itemname == $roleAll->name)
					{
						$flag = true;
						break;
					}
				}
				
				if($flag == false)
				{
					array_push($roleNew, $roleAll->name);
				}
				
			}
			
			$this->assignData('user', $user);
			$this->assignData('roles', $user_roles);
			$this->assignData('path', WebApp::app()->getBaseUrl());
			$this->assignData('rolesAll', $roleNew);
		}
	}

?>